function add_quick_history(history, example_node){
    //#当history已经存在时，需要判断是否变更状态，比如从running变为success，特殊处理一下
    if($('#history_' + history.id).size() > 0){
        if (example_node.parent().find('#history_' + history.id).size() == 0){
            $('#history_' + history.id)
                .prependTo(example_node.parent())
                .find('.progress').html(example_node.find('.progress').html())
        }
        return
    }
    //创建新的节点加入页面
    var node = example_node.clone()
    node.attr('id', 'history_' + history.id)
    node.find('.pull-right').text(history.start_time.substring(11,16) + '-' + (history.end_time ? history.end_time.substring(11,16) : '?'))
        .before(history.app_name + '(' + history.env + ')')
    node.children('a:first').attr('href', '/deploy_history/' + history.id)
        .attr("target", "_blank")
    node.prependTo(example_node.parent())
    node.show()

}

function create_node_by_example(example_node, history){
    var node = example_node.clone()
    node.attr('id', 'history_' + history.id)
    node.find('.pull-right').text(history.start_time.substring(11,16) + '-' + (history.end_time ? history.end_time.substring(11,16) : '?'))
        .before(history.app_name + '(' + history.env + ')')
    node.children('a:first').attr('href', '/deploy_history/' + history.id)
        .attr("target", "_blank")
    node.show()
    return node
}

function get_histories(){
        $.get( "/deploy_history/quick_index", function(histories) {
        //正在执行任务
        $("#running_num").text(histories[0].length)
        $("#running_describer").text(histories[0].length + '个正在执行的任务')
        $.each(histories[0], function(index, history){
            add_quick_history(history, $("#running_example"))
        })

        //今日成功任务
        $("#success_num").text(histories[1].length)
        $("#success_describer").text(histories[1].length + '个部署成功的任务')
        $.each(histories[1], function(index, history){
            add_quick_history(history, $("#success_example"))
        })

        //今日失败任务
        $("#failure_num").text(histories[9].length)
        $("#failure_describer").text(histories[9].length + '个部署失败的任务')
        $.each(histories[9], function(index, history){
            add_quick_history(history, $("#failure_example"))
        })

    });
}

$(document).ready(function(){
    get_histories()
    setInterval(get_histories, 5000)
})




